﻿// IO.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "IO.h"
#include "liwishell.h"
#include "Data.h"


// IO 对话框

IMPLEMENT_DYNAMIC(IO, CDialogEx)

IO::IO(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_IO, pParent)
{

}

IO::~IO()
{
}

void IO::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(IO, CDialogEx)
	ON_WM_CTLCOLOR()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_OUT0, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT1, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT2, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT3, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT4, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT5, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT6, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT7, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT8, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT9, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT10, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT11, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT12, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT13, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT14, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT15, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT16, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT17, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT18, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT19, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT20, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT21, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT22, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT23, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT24, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT25, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT26, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT27, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT28, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT29, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT30, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT31, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT32, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT33, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT34, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT35, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT36, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT37, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT38, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT39, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT40, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT41, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT42, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT43, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT44, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT45, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT46, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT47, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT48, &IO::SetOUT)
	ON_BN_CLICKED(IDC_OUT49, &IO::SetOUT)
END_MESSAGE_MAP()


// IO 消息处理程序

BOOL IO::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	// TODO:  在此添加额外的初始化

	for (int i = 0; i < 100; i++)
	{
		m_INControls[i].Attach(GetDlgItem(IDC_IN0 + i)->GetSafeHwnd());
		m_inNum[i] = 0;
	}
	for (int i = 0; i < 50; i++)
	{
		m_OUTLightControls[i].Attach(GetDlgItem(IDC_OUTLight0 + i)->GetSafeHwnd());
		m_outNum[i] = 0;
	}

	m_nTimerID = SetTimer(1, 200, NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

HBRUSH IO::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if (Data::ioFlag)
	{
		HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

		if (Data::CardIsConnect() && Data::ioFlag)
		{
			uint32 valueIN = 0;
			uint32 valueOUT = 0;
			//IN
			for (int i = 0; i < 100; i++)
			{
				if (pWnd->GetSafeHwnd() == m_INControls[i].GetSafeHwnd())
				{
					//i = pWnd->GetDlgCtrlID() - IDC_IN0;

					PMC_GetIn(Data::handle, i, &valueIN);
					m_inNum[i] = valueIN;
					if (valueIN == 0)
					{
						pDC->SetBkColor(RGB(125, 125, 125));
					}
					else
					{
						pDC->SetBkColor(RGB(255, 0, 0));
					}
					hbr = CreateSolidBrush(RGB(255, 255, 255));
					return hbr;
				}
			}
			//OUTLight
			for (int i = 0; i < 50; i++)
			{
				if (pWnd->GetSafeHwnd() == m_OUTLightControls[i].GetSafeHwnd())
				{
					//i = pWnd->GetDlgCtrlID() - IDC_IN0;

					PMC_GetOp(Data::handle, i, &valueOUT);
					m_outNum[i] = valueOUT;
					if (valueOUT == 0)
					{
						pDC->SetBkColor(RGB(255, 255, 255));
					}
					else
					{
						pDC->SetBkColor(RGB(255, 0, 0));
					}
					hbr = CreateSolidBrush(RGB(255, 255, 255));
					return hbr;
				}
			}

		}
		return hbr;
	}
}




//更新控件IN
void IO::OnTimer(UINT_PTR nIDEvent)
{
	if (Data::ioFlag)
	{

		// TODO: 在此添加消息处理程序代码和/或调用默认值
		//Sleep(200);
		if (nIDEvent == m_nTimerID)
		{
			for (int i = 0; i < 100; i++)
			{
				m_INControls[i].Invalidate();
			}
			for (int i = 0; i < 50; i++)
			{
				m_OUTLightControls[i].Invalidate();
			}
		}
	}
	CDialogEx::OnTimer(nIDEvent);
}

#pragma region 设置信号输出
/// <summary>
/// 设置信号输出
/// </summary>
/// <param name="i">对应IO</param>
void IO::SetOUT()
{
	CWnd* pWnd = GetFocus();
	int a = 999;	//焦点位置
	if (pWnd!=nullptr)
	{		
		UINT nID = pWnd->GetDlgCtrlID();	//获取控件ID
		switch (nID)
		{
			case IDC_OUT0:
				a = 0;
				break;
			case IDC_OUT1:
				a = 1;
				break;
			case IDC_OUT2:
				a = 2;
				break;
			case IDC_OUT3:
				a = 3;
				break;
			case IDC_OUT4:
				a = 4;
				break;
			case IDC_OUT5:
				a = 5;
				break;
			case IDC_OUT6:
				a = 6;
				break;
			case IDC_OUT7:
				a = 7;
				break;
			case IDC_OUT8:
				a = 8;
				break;
			case IDC_OUT9:
				a = 9;
				break;
			case IDC_OUT10:
				a = 10;
				break;
			case IDC_OUT11:
				a = 11;
				break;
			case IDC_OUT12:
				a = 12;
				break;
			case IDC_OUT13:
				a = 13;
				break;
			case IDC_OUT14:
				a = 14;
				break;
			case IDC_OUT15:
				a = 15;
				break;
			case IDC_OUT16:
				a = 16;
				break;
			case IDC_OUT17:
				a = 17;
				break;
			case IDC_OUT18:
				a = 18;
				break;
			case IDC_OUT19:
				a = 19;
				break;
			case IDC_OUT20:
				a = 20;
				break;
			case IDC_OUT21:
				a = 21;
				break;
			case IDC_OUT22:
				a = 22;
				break;
			case IDC_OUT23:
				a = 23;
				break;
			case IDC_OUT24:
				a = 24;
				break;
			case IDC_OUT25:
				a = 25;
				break;
			case IDC_OUT26:
				a = 26;
				break;
			case IDC_OUT27:
				a = 27;
				break;
			case IDC_OUT28:
				a = 28;
				break;
			case IDC_OUT29:
				a = 29;
				break;
			case IDC_OUT30:
				a = 30;
				break;
			case IDC_OUT31:
				a = 31;
				break;
			case IDC_OUT32:
				a = 32;
				break;
			case IDC_OUT33:
				a = 33;
				break;
			case IDC_OUT34:
				a = 34;
				break;
			case IDC_OUT35:
				a = 35;
				break;
			case IDC_OUT36:
				a = 36;
				break;
			case IDC_OUT37:
				a = 37;
				break;
			case IDC_OUT38:
				a = 38;
				break;
			case IDC_OUT39:
				a = 39;
				break;
			case IDC_OUT40:
				a = 40;
				break;
			case IDC_OUT41:
				a = 41;
				break;
			case IDC_OUT42:
				a = 42;
				break;
			case IDC_OUT43:
				a = 43;
				break;
			case IDC_OUT44:
				a = 44;
				break;
			case IDC_OUT45:
				a = 45;
				break;
			case IDC_OUT46:
				a = 46;
				break;
			case IDC_OUT47:
				a = 47;
				break;
			case IDC_OUT48:
				a = 48;
				break;
			case IDC_OUT49:
				a = 49;
				break;
			default:
				break;
		}
	}
	if (a!=999)
	{
		int ret= PMC_SetOp(Data::handle, a, m_outNum[a] == 0 ? 1 : 0);
		if (ret!=0)	MessageBox(TEXT("设置输出错误！！！"));
	}
	else
	{
		MessageBox(TEXT("点击按钮错误！！！"));
	}
	
}
#pragma endregion






